package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseDepartment;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseOfficeRepository;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.query.pageparam.OfficePageParam;
import com.wgchao.identityrightsmanagement.query.vo.DepartmentVo;
import com.wgchao.identityrightsmanagement.query.vo.OfficeVo;
import com.wgchao.identityrightsmanagement.query.vo.OfficeVoConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: OfficeQuery
 * @author: wgchao
 * @createTime: 2020/6/17 9:02 AM
 * @Description: TODO
 */
@Service
public class OfficeQuery {
    @Autowired
    private SysBaseOfficeRepository sysBaseOfficeRepository;
    @Autowired
    private OfficeVoConvert officeVoConvert;

    /**
     * 通过编号查询职位信息
     * @param code
     * @return
     */
    public OfficeVo findByCode(String code){
        SysBaseOffice sysBaseOffice = sysBaseOfficeRepository.findFirstByCodeAndDelFalse(code);
        if(sysBaseOffice == null){
            return null;
        }
        return officeVoConvert.convert(sysBaseOffice);
    }

    /**
     * 分页条件查询
     * @param officePageParam
     * @return
     */
    public PageData<OfficeVo> findByConditionsWithPage(OfficePageParam officePageParam){
        Specification<SysBaseOffice> specification = new Specification<SysBaseOffice>() {
            @Override
            public Predicate toPredicate(Root<SysBaseOffice> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isEmpty(officePageParam.getCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("code"), officePageParam.getCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(officePageParam.getAgencyCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("agencyCode"), officePageParam.getAgencyCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(officePageParam.getName())){
                    Predicate predicate = criteriaBuilder.like(root.get("name"), "%"+officePageParam.getName()+"%");
                    predicateList.add(predicate);
                }
                if(officePageParam.getDisabled() != null){
                    if(officePageParam.getDisabled()){
                        Predicate predicate = criteriaBuilder.isTrue(root.get("disabled"));
                        predicateList.add(predicate);
                    }else{
                        Predicate predicate = criteriaBuilder.isFalse(root.get("disabled"));
                        predicateList.add(predicate);
                    }
                }
                Predicate predicate = criteriaBuilder.isFalse(root.get("del"));
                predicateList.add(predicate);

                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };
        Pageable pageable = PageRequest.of(officePageParam.getCurr() - 1, officePageParam.getLimit(), Sort.by(Sort.Order.desc("createTime")));

        Page<SysBaseOffice> officePage = sysBaseOfficeRepository.findAll(specification, pageable);

        if(officePage.isEmpty()) {
            return new PageData<>(0L, officePageParam.getCurr(), null);
        }
        List<OfficeVo> officeVoList = officePage.get().map(sysBaseOffice -> {
            return officeVoConvert.convert(sysBaseOffice);
        }).collect(Collectors.toList());

        return new PageData<>(officePage.getTotalElements(), officePageParam.getCurr(), officeVoList);
    }
}
